<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2221 – This pattern always overrides a later one.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2221 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2221">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="this-pattern-always-overrides-a-later-one">This pattern always
overrides a later one.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2221.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="cf">case</span> <span class="st">&quot;</span><span class="va">$1</span><span class="st">&quot;</span> <span class="kw">in</span></span>
<span id="cb1-2"><a href="SC2221.html#cb1-2" aria-hidden="true" tabindex="-1"></a>  <span class="ss">-</span><span class="pp">?</span><span class="kw">)</span> <span class="bu">echo</span> <span class="st">&quot;Usage: </span><span class="va">$0</span><span class="st"> [-n]&quot;</span><span class="cf">;;</span></span>
<span id="cb1-3"><a href="SC2221.html#cb1-3" aria-hidden="true" tabindex="-1"></a>  <span class="ss">-n</span><span class="kw">)</span> <span class="bu">echo</span> <span class="st">&quot;Hello World&quot;</span><span class="cf">;;</span></span>
<span id="cb1-4"><a href="SC2221.html#cb1-4" aria-hidden="true" tabindex="-1"></a>   <span class="pp">*</span><span class="kw">)</span> <span class="bu">exit</span> 1<span class="cf">;;</span></span>
<span id="cb1-5"><a href="SC2221.html#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="cf">esac</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2221.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="cf">case</span> <span class="st">&quot;</span><span class="va">$1</span><span class="st">&quot;</span> <span class="kw">in</span></span>
<span id="cb2-2"><a href="SC2221.html#cb2-2" aria-hidden="true" tabindex="-1"></a>  <span class="ss">-</span><span class="dt">\?</span><span class="kw">)</span> <span class="bu">echo</span> <span class="st">&quot;Usage: </span><span class="va">$0</span><span class="st"> [-n]&quot;</span><span class="cf">;;</span></span>
<span id="cb2-3"><a href="SC2221.html#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="co"># &#39;-?&#39;) echo &quot;Usage: $0 [-n]&quot;;;  # Also valid</span></span>
<span id="cb2-4"><a href="SC2221.html#cb2-4" aria-hidden="true" tabindex="-1"></a>  <span class="ss">-n</span><span class="kw">)</span> <span class="bu">echo</span> <span class="st">&quot;Hello World&quot;</span><span class="cf">;;</span></span>
<span id="cb2-5"><a href="SC2221.html#cb2-5" aria-hidden="true" tabindex="-1"></a>   <span class="pp">*</span><span class="kw">)</span> <span class="bu">exit</span> 1<span class="cf">;;</span></span>
<span id="cb2-6"><a href="SC2221.html#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="cf">esac</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>You have specified multiple patterns in a <code>case</code>
statement, where one will always override the other. The pattern being
overridden is indicated with a <a href="SC2222.html">SC2222</a> warning.</p>
<p>In the example, <code>-?</code> actually matches a dash followed by
any character, such as <code>-n</code>. This means that the later
<code>-n</code> branch will never trigger. In this case, the correct
solution is to escape the <code>-\?</code> so that it doesn't match
<code>-n</code>.</p>
<p>Another common reason for this is accidentally duplicating a branch.
In this case, fix or delete the duplicate branch.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>None. One could argue that having
<code>-*|--*) echo "Invalid flag";</code> is a readability issue, even
though the second pattern follows from the first. In this case, you can
either rearrange the pattern from most to least specific, i.e.
<code>--*|-*)</code> or ignore the error.</p>
<p>When ignoring this error, remember that ShellCheck directives have to
go in front of the <code>case</code> statement, and not in front of the
branch:</p>
<pre><code># shellcheck disable=SC2221,SC2222
case &quot;$1&quot; in
  -n) ...;;
  # no directive here
  -*|--*) echo &quot;Unknown flag&quot; ;;
esac</code></pre>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


